package com.mxsat.system.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mxsat.common.annotation.ResponseResult;
import com.mxsat.common.result.PlatformResult;
import com.mxsat.system.entity.SysRole;
import com.mxsat.system.service.SysRoleService;

@ResponseResult(PlatformResult.class)
@RestController
@RequestMapping("/*")
public class SystemRoleController {
	
	@Autowired
	private SysRoleService sysRoleService;
	
	@GetMapping("/roles")
	public Page<SysRole> Roles(@RequestParam(name="page",required=true) int page,
							   @RequestParam(name="limit",required=true) int limit,
							   @RequestParam(name="keyword",required=false) String keyword) throws Exception {
		Page<SysRole> _page = new Page<>();
		_page.setPages(page);
		_page.setSize(limit);
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("keyword", keyword);
		return (Page<SysRole>) sysRoleService.findSysRolesByPage(_page ,condition);
	}
	
	@GetMapping("/role/{rid}")
	public SysRole Role(@PathVariable Long rid) throws Exception{
		return sysRoleService.findSysRoleById(rid);
	}
	
	@PostMapping("/role")
	public boolean addRole(SysRole role) throws Exception{
		role.setId_creator(1l);
		role.setId_updater(1l);
		return sysRoleService.addSysRole(role);
	}
	
	@PutMapping("/role")
	public boolean modifyRole(SysRole role) throws Exception{
		return sysRoleService.modifySysRoleById(role);
	}
	
	@DeleteMapping("/role/{rid}")
	public boolean removeyRole(@PathVariable Long rid) throws Exception{
		return sysRoleService.removeSysRoleById(rid);
	}
}
